package com.asset.dispoal.file.agreementCaseData.service.commom;

import com.asset.dispoal.business.idcardrefresh.domain.db.mapper.IdCardRefreshMapper;
import com.asset.dispoal.business.idcardrefresh.domain.dto.IdCardRefreshHistoryCaseDTO;
import com.asset.dispoal.file.agreementCaseData.domain.dto.ExportIdCardRefresh;
import com.asset.dispoal.file.common.ExcelExport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.springboot.core.spring.util.DataUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.io.File;
import java.util.List;

@Component
@Slf4j
public class ExportNoRefreshIdCardCommand implements BaseCommand<String, Result> {

    @Autowired
    Environment environment;

    @Autowired
    IdCardRefreshMapper idCardRefreshMapper;

    @Override
    public Result execute(String id) {
        String headUrl = environment.getProperty("file.evidence.url");
        String appendUrl = environment.getProperty("file.documentType.url");
        String property = "/" + appendUrl + "/" + id + ".xlsx";
        File file = new File(headUrl + property);
        List<IdCardRefreshHistoryCaseDTO> dtoList = idCardRefreshMapper.queryNoCardHistoryCaseDTO(id);
        if (CollectionUtil.isEmpty(dtoList)) {
            return Result.faild("当前不存在对应数据");
        }
        List<ExportIdCardRefresh> exportDataList = DataUtil.copyTo(dtoList, ExportIdCardRefresh.class);
        ExcelExport.export(file, exportDataList, ExportIdCardRefresh.class);
        return Result.ok(property);
    }
}
